package fr.imag.ufrima.ima.project.ui.component;

import java.awt.Component;

import org.noos.xing.mydoggy.Content;
import org.noos.xing.mydoggy.ContentManager;
import org.noos.xing.mydoggy.DockedTypeDescriptor;
import org.noos.xing.mydoggy.ToolWindow;
import org.noos.xing.mydoggy.ToolWindowAnchor;
import org.noos.xing.mydoggy.ToolWindowManager;
import org.noos.xing.mydoggy.ToolWindowType;
import org.noos.xing.mydoggy.plaf.MyDoggyToolWindowManager;

import fr.imag.ufrima.ima.project.ui.main.ImageProject;
import fr.imag.ufrima.ima.project.ui.tools.FilterTool;
import fr.imag.ufrima.ima.project.ui.tools.FlouTool;
import fr.imag.ufrima.ima.project.ui.tools.FusionTool;
import fr.imag.ufrima.ima.project.ui.tools.HistogramTool;
import fr.imag.ufrima.ima.project.ui.tools.Outils;
import fr.imag.ufrima.ima.project.ui.tools.ResizeTool;
import fr.imag.ufrima.ima.project.ui.tools.SharpenTool;

public class ToolsPanel {
	private ImageProject imageProject;

	private ToolWindowManager toolWindowManager;
	private ToolWindow toolWindowUtils;
	private ToolWindow toolWindowResize;
	private ToolWindow toolWindowFilter;
	private ToolWindow toolWindowFlou;
	private ToolWindow toolWindowFusion;
	private ToolWindow toolWindowHistogram;
	private ToolWindow toolWindowSharpen;

	public ToolsPanel(ImageProject imageProject, MyDoggyToolWindowManager myDoggyToolWindowManager) {
		this.imageProject = imageProject;
		initToolWindowManager(myDoggyToolWindowManager);
	}

	private void initToolWindowManager(MyDoggyToolWindowManager myDoggyToolWindowManager) {
		// Create a new instance of MyDoggyToolWindowManager passing the frame.
		this.toolWindowManager = myDoggyToolWindowManager;

		// Register the tool box for Outils
		toolWindowUtils = toolWindowManager.registerToolWindow("Outils", "Outils", null, new Outils(this.imageProject),
				ToolWindowAnchor.RIGHT);

		// Register the tool box for Histogram and modify box
		HistogramTool histogramTool = new HistogramTool(imageProject);
		toolWindowHistogram = toolWindowManager.registerToolWindow("Histrogramme", "Histrogramme", null, histogramTool,
				ToolWindowAnchor.RIGHT);
		toolWindowHistogram.addPropertyChangeListener(histogramTool.getPropertyChangeListener());
		DockedTypeDescriptor descriptorHistogram = (DockedTypeDescriptor) toolWindowHistogram
				.getTypeDescriptor(ToolWindowType.DOCKED);
		descriptorHistogram.setDockLength(350);

		// Register the tool box for Fusion
		FusionTool fusionTool = new FusionTool(this.imageProject);
		toolWindowFusion = toolWindowManager.registerToolWindow("Fusion", "Fusion", null, fusionTool,
				ToolWindowAnchor.RIGHT);

		// Configure property listener and modify witdh of Fusion Box
		toolWindowFusion.addPropertyChangeListener(fusionTool.getPropertyChangeListener());
		DockedTypeDescriptor descriptorFusion = (DockedTypeDescriptor) toolWindowFusion
				.getTypeDescriptor(ToolWindowType.DOCKED);
		descriptorFusion.setDockLength(250);

		// Register the tool box for Resize
		ResizeTool resizeTool = new ResizeTool(this.imageProject);
		toolWindowResize = toolWindowManager.registerToolWindow("Redimensionner", "Redimensionner", null, resizeTool,
				ToolWindowAnchor.RIGHT);

		// Configure property listener for Resize Box
		toolWindowResize.addPropertyChangeListener(resizeTool.getPropertyChangeListener());

		// Register the tool box for Flou
		FlouTool flouTool = new FlouTool(this.imageProject);
		toolWindowFlou = toolWindowManager.registerToolWindow("Flou", "Flou", null, flouTool, ToolWindowAnchor.RIGHT);

		// Register the tool box for Filter
		FilterTool filterTool = new FilterTool(this.imageProject);
		toolWindowFilter = toolWindowManager.registerToolWindow("Filtre", "Filtre", null, filterTool,
				ToolWindowAnchor.RIGHT);

		// Register the tool box for Sharpen
		SharpenTool sharpenTool = new SharpenTool(this.imageProject);
		toolWindowSharpen = toolWindowManager.registerToolWindow("Sharpen", "Sharpen", null, sharpenTool,
				ToolWindowAnchor.RIGHT);
	}

	public void setAllToolsAvailability(boolean availability) {

		for (ToolWindow window : toolWindowManager.getToolWindows())
			window.setAvailable(availability);
	}

	public void hideAll() {
		for (ToolWindow window : toolWindowManager.getToolWindows()) {
			if (!window.getTitle().equals("Outils")) {
				window.setVisible(false);
			}
		}
	}

	public void initContentManager(Component theDesktop) {
		ContentManager contentManager = toolWindowManager.getContentManager();
		Content content = contentManager.addContent("Content", "Content", null, theDesktop);
	}

	public ToolWindow getToolWindowUtil() {
		return toolWindowUtils;
	}

	public ToolWindow getToolWindowResize() {
		return toolWindowResize;
	}

	public ToolWindow getToolWindowFlou() {
		return toolWindowFlou;
	}

	public ToolWindow getToolWindowFilter() {
		return toolWindowFilter;
	}

	public ToolWindow getToolWindowFusion() {
		return toolWindowFusion;
	}

	public ToolWindow getToolWindowHistogram() {
		return toolWindowHistogram;
	}
	
	public ToolWindow getToolWindowSharpen() {
		return toolWindowSharpen;
	}
}
